package com.app.Controller;

import java.util.ArrayList;

import it.unipr.ce.dsg.s2p.message.Payload;
import it.unipr.ce.dsg.s2p.sip.Address;

import android.app.Activity;
import android.app.Application;
import android.graphics.YuvImage;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;

import com.app.Activity.frmBieuQuyetMoiRoiNhom;
import com.app.Activity.frmMainActivity;
import com.app.Activity.trucActivity;
import com.app.DTO.AccountDTO;
import com.app.DTO.RoleDTO;
import com.app.Network.NetworkLayer;
import com.app.Network.PingMessage;
import com.app.Network.Protocol;

public class NetworkController {

	public static NetworkLayer _peer=null;
	public static Handler handler=new Handler();
	

	

	public static boolean sendGroupInvite(Address toAddress){

		boolean rs=_peer.getAccount().getRule().get(0).getSend();
		if(rs){
			PingMessage message=new PingMessage(_peer.getPeerDescriptor());
			message.setType(Protocol.GROUP_INVITE);
			_peer.send(toAddress, message);
			Log.d("Phu", "Da send");
		}
		else{
			handler.post(new Runnable() {
				
				@Override
				public void run() {
					ActivityController.showError();
					
				}
			});
			
		}
		return rs;
	}
	public static boolean sendDongYVaoNhom(Address toAddress){
		PingMessage msg=new PingMessage(_peer.getPeerDescriptor());
		msg.setType(Protocol.GROUP_INVITE_REPLY_YES);
		_peer.send(new Address(toAddress), msg);
		
		return true;
	}
	public static boolean sendKhongChoVaoNhom(Address toAddress){
		PingMessage msg=new PingMessage(_peer.getPeerDescriptor());
		msg.setType(Protocol.GROUP_INVITE_NO);
		_peer.send(new Address(toAddress), msg);		
		return true;
	}
	public static boolean sendChoVaoNhom(Address toAddress) {
		PingMessage msg=new PingMessage(_peer.getPeerDescriptor());
		msg.setType(Protocol.GROUP_INVITE_YES);
		_peer.send(new Address(toAddress), msg);		
		return true;
		
	}
	public static boolean sendSetRole(Address toAddress, ArrayList<RoleDTO> roleList){
		PingMessage msg=new PingMessage(_peer.getPeerDescriptor());
		
		Payload payload=new Payload();
		payload.addParam("DATA", RoleDTO.toStringList(roleList));		
		msg.setType(Protocol.SET_ROLE);
		msg.setPayload(payload);
		_peer.send(toAddress, msg);
		return true;
	}

	//========================================truc===============================================
	public static void SendHelloDLG(Address toAdd){
		PingMessage m=new PingMessage(_peer.getPeerDescriptor());
		m.setType(Protocol.HELLO);
		_peer.send(toAdd, m);
		
	}
	
	public static void ShowForm1(){
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				ActivityController.ShowFrm1();
			}
		});
	}
	
	public static void ShowForm2(){
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				ActivityController.ShowFrm2();
			}
		});
	}
	
	//tao lao================
	
	
	//Quyền : 0:Chat, 1:Cam ngkhac noi, 2: moi nguoi khac ra khoi nhom
	public static void ShowMessageNotice(final String message){
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
			
			ActivityController.ShowMessage(message);
			}
		});
	}
	
	public static void ShowMessageChat(final String content, final String Address){
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
			
			ActivityController.ShowChatMessage(content,Address);
			}
		});
	}
	
	public static void DislayVoteKickMember(){
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				ActivityController.DislayKickMember();
			}
		});
	}
	
	public static void DislayResultVoteKick(){
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				ActivityController.ShowResultVote();
			}
		});
	}
	
	public static void SetTimeKick(final int time){
		ActivityController.SetTimeVote(time);
	}
	
	public static void doEndTimeKick(){
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
			ActivityController.DoEndTime();	
			}
		});
	}
	
	public static void GuiYeuCauNgungNoi(final Address toAdd,int time){
		Log.d("Phu","vao Controller ngung noi");
		boolean YourRule=_peer.getAccount().getRule().get(1).getSend();
		if (YourRule) {
			Log.d("Phu","vao if ngung noi");
			PingMessage mess=new PingMessage(_peer.getPeerDescriptor());
			mess.setType(Protocol.STOP_TALKING_ONE_MEMBER);
			Payload pl=new Payload();
			pl.addParam("TimeStopTalk", time);
			mess.setPayload(pl);
			//String message="Ban da bi ngung noi";
			_peer.send(new Address(toAdd), mess);
			
			Log.d("Phu","gui yeu cau ngung noi");
		}else
		{
			Log.d("Phu","ko get dc quyen");
			NetworkController.ShowMessageNotice(Protocol.NON_RULE_MESSAGE);
		}
	}
	
	public static void GuiChoPhepNoiChuyen(Address toAddress){
		Log.d("Phu","vao Controller cho phep noi");
		boolean YourRule=_peer.getAccount().getRule().get(1).getSend();
		if(YourRule){
			Log.d("Phu","vao if cho noi");
			PingMessage message=new PingMessage(_peer.getPeerDescriptor());
			message.setType(Protocol.ACCEPT_TALKING_ONE_MEMBER);
			_peer.send(new Address(toAddress), message);
			Log.d("Phu","gui yeu cau cho noi");
		}else{
			Log.d("Phu","ko get dc quyen");
			NetworkController.ShowMessageNotice(Protocol.NON_RULE_MESSAGE);
		}
	}
	
	public static void GuiTinNhanChat(Address toAddress, String content){
		boolean YourRule=_peer.getAccount().getRule().get(0).getSend();
		if (YourRule) {
			Log.d("Phu","vao if gui tin nhan");
			PingMessage mess=new PingMessage(_peer.getPeerDescriptor());
			mess.setType(Protocol.CHAT_MESSAGE);
			Payload pl=new Payload();
			pl.addParam("Content_Chat", content);
			mess.setPayload(pl);
			//String message="Ban da bi ngung noi";
			_peer.send(new Address(toAddress), mess);
			ActivityController.ShowChatMessage(content,_peer.getAddressPeer().toString());
			Log.d("Phu","gui tin nhan");
		}else
		{
			Log.d("Phu","ko get dc quyen");
			NetworkController.ShowMessageNotice(Protocol.NON_RULE_MESSAGE);
		}
	}
	
	public static void GuiPhanHoiKhongCoQuyenChat(Address toAddress){
		Log.d("Phu","gui phan hoi ko co quyen chat");
		PingMessage message=new PingMessage(_peer.getPeerDescriptor());
		message.setType(Protocol.CHAT_FEDBACK_NON_RULE);
		Payload pl=new Payload();
		//String s=_peer.getAccount().get_Username().toString();
		//pl.addParam("Content", s+" không có quyền nhận tin nhắn trò chuyện.");
		pl.addParam("Content", "đối phương không có quyền nhận tin nhắn trò chuyện.");
		message.setPayload(pl);
		_peer.send(new Address(toAddress), message);
	}
	
	
	//gửi thông báo cần biểu quyết để mời 1 thành viên ra khỏi nhóm
	public static void SendVoteKickMember(Address toAddress, Address beingKick, int time){
		Log.d("Phu","vao send vote");
		ActivityController.ShowMessage("da gui");
		boolean yourRule=_peer.getAccount().getRule().get(2).getSend();
		if(yourRule){
			Log.d("Phu","vao if send vote");
			PingMessage message=new PingMessage(_peer.getPeerDescriptor());
			message.setType(Protocol.KICK_MEMBER_OUT_MESSAGE);
			Payload pl=new Payload();
			
			pl.addParam("Time", time);
			pl.addParam("IPBeingKick", beingKick.getURL());
			Log.d("Phu","ip bi kick "+beingKick.getURL());
			//khi nao co dang nhap thi moi them vao
			
		//	pl.addParam("accUserName", beingKick.getAccount().get_Username());
			
			message.setPayload(pl);
			_peer.send(toAddress, message);
			Log.d("Phu","da send vote");
		}
		else
		{
			NetworkController.ShowMessageNotice(Protocol.NON_RULE_MESSAGE);
		}
	}
	//gửi ý kiến biểu quyết về việc mời 1 thành viên ra khỏi nhóm
	public static void SendFedbackKickMember(Address toAddress,int vote){
		Log.d("Phu","vao ham bieu quyet kick");
		//int vote=frmBieuQuyetMoiRoiNhom.YKienBieuQuyet;
		Log.d("Phu","bieu quyet kick = "+vote);
		boolean yourRule=_peer.getAccount().getRule().get(3).getSend();
		if(yourRule)
		{
			Log.d("Phu","vao if bieu quyet kick");
			PingMessage message=new PingMessage(_peer.getPeerDescriptor());
			message.setType(Protocol.KICK_MEMBER_OUT_FEDBACK);
			Payload pl=new Payload();
			if(vote==1){
				Log.d("Phu"," bieu quyet kick = yes");
				pl.addParam("VOTE", "1");
			}
			else if(vote==0)
			{
				Log.d("Phu"," bieu quyet kick = no");
				pl.addParam("VOTE", "0");
			}
			else
			{
				Log.d("Phu"," bieu quyet kick = khac");
			}
			message.setPayload(pl);
			_peer.send(toAddress, message);
		}
		else
		{
			
			NetworkController.ShowMessageNotice("bạn không có quyền biểu quyết kick");
			Log.d("Phu","ko co quyen bieu quyet kick");
		}
	}
	
	
	//hàm gửi kết quả cuoi cung ve moi nguoi khac ra khoi nhom
	public static void SendResultVoteKickMember(Address toAdd,int result,Address beingKickAdd){
		boolean yourRule=_peer.getAccount().getRule().get(3).getSend();
		if(yourRule)
		{
			Log.d("Phu","vao if send result kick");
			PingMessage message=new PingMessage(_peer.getPeerDescriptor());
			message.setType(Protocol.KICK_MEMBER_OUT_RESULT);
			Payload pl=new Payload();
			
			
			
			pl.addParam("Result", result);
			pl.addParam("BeingKick",beingKickAdd.getURL());
			message.setPayload(pl);
			_peer.send(toAdd, message);
			Log.d("Phu","da send result kick");
		}
	}
	

	public static void CloseActivity(){
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				ActivityController.FinishDiscussActivity();
			}
		});
	}
	
		//===========================================end truc==================================================
		

	public static void init(String name, int port,AccountDTO account)
	{
		_peer = new NetworkLayer(null,"4654amv65d4as4d65a4", name, port,account);
	}

	public static void resetRole(){
		_peer.account.setRule(null);
		_peer.account.setRoleList(null);
		ArrayList<RoleDTO> roleList=ReadWriteFileController.readRole(ReadWriteFileController.DIRECTORY+"myRole.xml");
		for(RoleDTO role:roleList){
			_peer.account.addRole(role);
		}
	}


}

